package com.example.demo.algorithm.lesson.basic.class1;

import com.example.demo.algorithm.lesson.basic.class3.MergeSort;
import com.sun.scenario.effect.Merge;

import java.util.Arrays;

/**
 * @author liweizhi
 * @date 2020/4/22 16:17
 */
public class ArrayCompareHelper {
    // 对数器
    public static void comparator(int[] arr) {
        Arrays.sort(arr);
    }

    public static int[] newRandomArray(int size, int max, int min) {
        int[] arr = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = (int) Math.floor(Math.random() * (max - min + 1)) + min;
        }
        return arr;
    }

    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] ret = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ret[i] = arr[i];
        }
        return ret;
    }

    public static boolean checkEquals(int[] a, int[] s) {
        if ((a == null && s != null) || (a != null && s == null)) {
            return false;
        }
        // a == null && s == null
        if (a == null) {
            return true;
        }
        if (a.length != s.length) {
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            if (a[i] != s[i]) {
                return false;
            }
        }
        return true;
    }

    public static void printArray(int[] arr) {
        if (arr == null || arr.length == 0) {
            return;
        }
        int maxIndex = arr.length - 1;
        for (int i = 0; i <= maxIndex; i++) {
            System.out.print(arr[i]);
            if (i < maxIndex) {
                System.out.print(",");
            }
        }
        System.out.println();
    }

    public static void swap(int[] arr, int i, int j) {
        if (i == j) {
            return;
        }
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args) {
        int tryTimes = 30_0000;
        int size = 100;
        int max = 50;
        int min = -50;
        boolean succeed = true;
        for (int i = 0; i < tryTimes; i++) {
            int[] arr1 = newRandomArray(size, max, min);
            int[] arr2 = copyArray(arr1);
            MergeSort.sort2(arr1);
            comparator(arr2);
            if (!checkEquals(arr1, arr2)) {
                succeed = false;
                printArray(arr1);
                printArray(arr2);
                break;
            }
        }
        System.out.println(succeed ? "Yes" : "No");
    }
}
